# Replication data for
# Kathleen Gallagher Cunningham and Nils B. Weidmann 
# "Shared Space: Ethnic Groups, State Accommodation, 
# and Localized Conflict"
# International Studies Quarterly (2010) 54, 1035–1054

library(Design)

uldata <- read.csv("replication.csv")

######
# State-directed violence in unit
######
# Model 1 (all)
m1 <- lrm(localconf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG) + lunitpop + lcgdp + polity2 + lctrpop + federal, data=uldata, x=T, y=T)
m1 <- robcov(m1, uldata$COWCODE)
m1


# Model 2 (fiscally decentralized)
m2data <- uldata[uldata$decent==1,]
m2 <- lrm(localconf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG) + lunitpop + lcgdp + polity2 + lctrpop + federal, data=m2data, x=T, y=T)
m2 <- robcov(m2, m2data$COWCODE)
m2

######
# Combined violence (state-directed and communal)
######
# Model 3 (all)
m3 <- lrm(conf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG) + lunitpop  + lcgdp + polity2 + lctrpop + federal, data=uldata, x=T, y=T)
m3 <- robcov(m3, uldata$COWCODE)
m3

# Model 4 (fiscally decentralized)
m4data <- uldata[uldata$decent==1,]
m4 <- lrm(conf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG)  + lunitpop  + lcgdp + polity2 + lctrpop + federal, data=m4data, x=T, y=T)
m4 <- robcov(m4, m4data$COWCODE)
m4


# plot of results
m1coeffs <- m1$coeff
m2coeffs <- m2$coeff
m3coeffs <- m3$coeff
m4coeffs <- m4$coeff

m1data <- model.frame(m1)
m2data <- model.frame(m2)
m3data <- model.frame(m3)
m4data <- model.frame(m4)

invlogit <- function(x) {
exp(x)/(1+exp(x))	
}

curve(invlogit(m1coeffs[1]+m1coeffs[2]*x+m1coeffs[3]*x^2+m1coeffs[4]*1+m1coeffs[5]*mean(m1data$lunitpop)+m1coeffs[6]*0+m1coeffs[7]*mean(m1data$lcgdp)+m1coeffs[8]*mean(m1data$polity2)+m1coeffs[9]*mean(m1data$lctrpop)), from=0, to=1, xlab="Domination", ylab="Predicted conflict risk", ylim=c(0, 0.1), lwd=2, lty=1)

curve(invlogit(m2coeffs[1]+m2coeffs[2]*x+m2coeffs[3]*x^2+m2coeffs[4]*0+m2coeffs[5]*mean(m2data$lunitpop)+m2coeffs[6]*0+m2coeffs[7]*mean(m2data$lcgdp)+m2coeffs[8]*mean(m2data$polity2)+m2coeffs[9]*mean(m2data$lctrpop)), from=0, to=1, add=T, lwd=2, lty=2)

curve(invlogit(m3coeffs[1]+m3coeffs[2]*x+m3coeffs[3]*x^2+m3coeffs[4]*0+m3coeffs[5]*mean(m3data$lunitpop)+m3coeffs[6]*0+m3coeffs[7]*mean(m3data$lcgdp)+m3coeffs[8]*mean(m3data$polity2)+m3coeffs[9]*mean(m3data$lctrpop)), from=0, to=1, add=T, lwd=2, lty=3)

curve(invlogit(m4coeffs[1]+m4coeffs[2]*x+m4coeffs[3]*x^2+m4coeffs[4]*0+m4coeffs[5]*mean(m4data$lunitpop)+m4coeffs[6]*0+m4coeffs[7]*mean(m4data$lcgdp)+m4coeffs[8]*mean(m4data$polity2)+m4coeffs[9]*mean(m4data$lctrpop)), from=0, to=1, add=T, lwd=2, lty=4)

legend(0.76, 0.1, c("Model 1", "Model 2", "Model 3", "Model 4"), lty = c(1, 2, 3, 4), lwd=2)


#####
# Additional tests: control for prior ethnic conflict
#####
m1 <- lrm(localconf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG) + lunitpop + lcgdp + polity2 + lctrpop + federal + as.factor(ethwar==2), data=uldata, x=T, y=T)
m1 <- robcov(m1, uldata$COWCODE)
m1

m2data <- uldata[uldata$decent==1 & !is.na(uldata$ethwar),]
m2 <- lrm(localconf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG) + lunitpop + lcgdp + polity2 + lctrpop + federal + as.factor(ethwar==2), data=m2data, x=T, y=T)
m2 <- robcov(m2, m2data$COWCODE)
m2

m3 <- lrm(conf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG) + lunitpop  + lcgdp + polity2 + lctrpop + federal + as.factor(ethwar==2), data=uldata, x=T, y=T)
m3 <- robcov(m3, uldata$COWCODE)
m3

m4data <- uldata[uldata$decent==1 & !is.na(uldata$ethwar),]
m4 <- lrm(conf ~ DOMINATION + DOMINATION.sq + as.factor(LARGESTISMEG)  + lunitpop  + lcgdp + polity2 + lctrpop + federal + as.factor(ethwar==2), data=m4data, x=T, y=T)
m4 <- robcov(m4, m4data$COWCODE)
m4

